//
//  LoginViewController.m
//  moon
//
//  Created by beyself on 16/6/6.
//  Copyright © 2016年 beyself. All rights reserved.
//

#import "Utils.h"
#import "SmsCodeViewController.h"
#import "LoginViewController.h"
#import "ForgetPwdViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIButton* bg = [UIButton buttonWithType:UIButtonTypeCustom];
    bg.frame = CGRectMake(0, 0, SCREEN_WIDTH, 100);
    bg.enabled = NO;
    [bg setImage:[UIImage imageNamed:@"logo"] forState:UIControlStateDisabled];
    [self.view addSubview:bg];
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = -5;
    self.navigationItem.leftBarButtonItems = @[negativeSpacer,
                                               [[UIBarButtonItem alloc] initWithCustomView:[UIButton buttonWithType:UIButtonTypeCustom]]];
    
    self.title = @"登录";
    for(int i = 0; i < 2; i++)
    {
        UIView* subview = [[UIView alloc] init];
        subview.frame = CGRectMake(0, 100 + 49*i, SCREEN_WIDTH, 48);
        subview.backgroundColor = WHITE;
        
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 48, 48);
        button.enabled = NO;
        if(0 == i) [button setImage:[UIImage imageNamed:@"icon_phone"] forState:UIControlStateDisabled];
        else [button setImage:[UIImage imageNamed:@"icon_code"] forState:UIControlStateDisabled];
        [subview addSubview:button];
        
        UITextField* textfield = [[UITextField alloc] init];
        textfield.tag = 1000 + i;
        textfield.frame = CGRectMake(48, 0, SCREEN_WIDTH - 58, 48);
        textfield.textColor = BLACK;
        textfield.delegate = self;
        if(i == 0)
        {
            textfield.keyboardType = UIKeyboardTypePhonePad;
            textfield.placeholder = @"请输入您的手机号码";
        }
        else
        {
            textfield.keyboardType = UIKeyboardTypeEmailAddress;
            textfield.secureTextEntry = YES;
            textfield.placeholder = @"请输入您的密码";
        }
        [subview addSubview:textfield];
        [self.view addSubview:subview];
    }
    
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 210, SCREEN_WIDTH - 20, 44);
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    button.titleLabel.font = FONT_15;
    [button addTarget:self action:@selector(loginBtn) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[Utils imageFromColor:ORANGE] forState:UIControlStateNormal];
    [button setBackgroundImage:[Utils imageFromColor:LINE] forState:UIControlStateHighlighted];
    [button setTitle:@"登录" forState:UIControlStateNormal];
    [self.view addSubview:button];
    
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(SCREEN_WIDTH/2 + 10, 260, 64, 32);
    button.titleLabel.font = FONT_12;
    [button setTitle:@"忘记密码" forState:UIControlStateNormal];
    [button setTitleColor:BLUE forState:UIControlStateNormal];
    [button addTarget:self action:@selector(showForget) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UILabel* label = [[UILabel alloc] init];
    label.frame = CGRectMake(SCREEN_WIDTH/2, 266, 1, 20);
    label.backgroundColor = BLUE;
    [self.view addSubview:label];
    
    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(SCREEN_WIDTH/2 - 74, 260, 64, 32);
    button.titleLabel.font = FONT_12;
    [button setTitle:@"点击激活" forState:UIControlStateNormal];
    [button setTitleColor:BLUE forState:UIControlStateNormal];
    [button addTarget:self action:@selector(showRegister) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singleTap:)];
    [singleTapGestureRecognizer setNumberOfTapsRequired:1];
    [self.view addGestureRecognizer:singleTapGestureRecognizer];
}

- (void)singleTap:(UIGestureRecognizer*)gestureRecognizer
{
    UITextField* textfield = (UITextField*)[self.view viewWithTag:1000];
    [textfield resignFirstResponder];
    
    textfield = (UITextField*)[self.view viewWithTag:1001];
    [textfield resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void) showForget
{
    ForgetPwdViewController* subview = [[ForgetPwdViewController alloc] init];
    [self.navigationController pushViewController:subview animated:YES];
    
    UITextField* textfield = (UITextField*)[self.view viewWithTag:1000];
    [textfield resignFirstResponder];
    
    textfield = (UITextField*)[self.view viewWithTag:1001];
    [textfield resignFirstResponder];
}

- (void) showRegister
{
    SmsCodeViewController* subview = [[SmsCodeViewController alloc] init];
    [self.navigationController pushViewController:subview animated:YES];
    
    UITextField* textfield = (UITextField*)[self.view viewWithTag:1000];
    [textfield resignFirstResponder];
    
    textfield = (UITextField*)[self.view viewWithTag:1001];
    [textfield resignFirstResponder];
}

- (void) loginBtn
{
    NSString* tel = ((UITextField*)[self.view viewWithTag:1000]).text;
    NSString* pwd = ((UITextField*)[self.view viewWithTag:1001]).text;
    
    if(tel.length == 0 || pwd.length == 0)
    {
        [self showMsg:@"请输入手机号码/密码"];
        return;
    }
    
    NSMutableDictionary* param = [[NSMutableDictionary alloc] init];
    [param setObject:tel forKey:@"phonenumber"];
    [param setObject:pwd forKey:@"password"];
    if(APPDELEGATE.deviceToken.length) [param setObject:APPDELEGATE.deviceToken forKey:@"ios_token"];
    [Utils sendHttpPost:param method:@"member_login" controller:self flag:0];
    
    UITextField* textfield = (UITextField*)[self.view viewWithTag:1000];
    [textfield resignFirstResponder];
    
    textfield = (UITextField*)[self.view viewWithTag:1001];
    [textfield resignFirstResponder];
}

- (void) httpCallback:(NSDictionary*)data flag:(int)flag
{
    
    [Utils saveCustomObject:data[@"member"] key:LOGIN_INFO];
    
    NSString* tel = ((UITextField*)[self.view viewWithTag:1000]).text;
    NSString* pwd = ((UITextField*)[self.view viewWithTag:1001]).text;
    
    [Utils saveDefaultUser:tel key:@"username"];
    [Utils saveDefaultUser:pwd key:@"password"];
    
    [APPDELEGATE showMainView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
